package com.example.wpx.service.impl;

import com.example.wpx.dao.EntrustMapper;
import com.example.wpx.dao.UserMapper;
import com.example.wpx.pojo.Entrust;
import com.example.wpx.pojo.User;
import com.example.wpx.service.EntrustSerivice;
import com.example.wpx.util.EmptyUtil;
import com.example.wpx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EntrustServiceImpl implements EntrustSerivice {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EntrustMapper entrustMapper;
    @Override
    public ServerResponse add(String uid, String seviceType, String uname, String phone, String content,String money) {
        if(EmptyUtil.isEmpty(uid)
                ||EmptyUtil.isEmpty(uname)
                ||EmptyUtil.isEmpty(seviceType)
                ||EmptyUtil.isEmpty(phone)
                ||EmptyUtil.isEmpty(content) ){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        Entrust entrust = new Entrust();
        entrust.setContent(content);
        entrust.setuId(Integer.parseInt(uid));
        entrust.setName(uname);
        entrust.setType(seviceType);
        entrust.setPhone(phone);
        entrust.setStatus("1");
        entrust.setStuId(money);
        Integer index = entrustMapper.insertSelective(entrust);
        if(index>0){
            return ServerResponse.createBySuccessMessage("发布成功");
        }
        return ServerResponse.createByErrorMessage("发布失败");
    }

    @Override
    public ServerResponse get(String eid, String uid) {
        Entrust entrust = entrustMapper.selectByPrimaryKey(Integer.parseInt(eid));
        User user = userMapper.selectByPrimaryKey(Integer.parseInt(uid));
        if(EmptyUtil.isEmpty(entrust))return ServerResponse.createByErrorMessage("参数错误");
        if(EmptyUtil.isEmpty(user))return ServerResponse.createByErrorMessage("用户不存在");
        if(user.getId() == entrust.getuId())return ServerResponse.createByErrorMessage("不可以接自己发布的任务");
        entrust.seteUId(Integer.parseInt(uid));
        entrust.setStatus("2");
        Integer index = entrustMapper.updateByPrimaryKeySelective(entrust);
        if(index>0)return ServerResponse.createBySuccessMessage("接单成功");
        return ServerResponse.createByErrorMessage("接单失败");
    }

    @Override
    public ServerResponse allList(String serviceType) {
        if(EmptyUtil.isEmpty(serviceType)){
            serviceType = "";
        }
        List<Entrust> list  = entrustMapper.allList(serviceType);
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse myRelease(String uid) {
        List<Entrust> list  = entrustMapper.myRelease(Integer.parseInt(uid));
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse myGet(String uid) {
        List<Entrust> list  = entrustMapper.myGet(Integer.parseInt(uid));
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse finish(String eid, String uid) {
        if(EmptyUtil.isEmpty(eid) || EmptyUtil.isEmpty(uid)) return ServerResponse.createByErrorMessage("参数错误");
        Entrust entrust = entrustMapper.selectByPrimaryKey(Integer.parseInt(eid));
       // User user = userMapper.selectByPrimaryKey(Integer.parseInt(uid));
        if(entrust.getuId() != Integer.parseInt(uid)){
            return ServerResponse.createByErrorMessage("参数错误");
        }
        entrust.setStatus("3");
        Integer index = entrustMapper.updateByPrimaryKeySelective(entrust);
        if(index>0)return ServerResponse.createBySuccessMessage("恭喜你订单已成功");
        return ServerResponse.createByErrorMessage("哪里不对了~~~");
    }
}
